﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP_ForceDotCom_Pivot.Resources;

namespace WP_ForceDotCom_Pivot
{
    public partial class MainPage : PhoneApplicationPage
    {

        string headtitle = "FORCE.COM .NET WP";

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            auth.doauth(); // force.com authentication
            
            this.ForceComPivot.Title = headtitle + " :: " + Globals.sessionstatus;

            if (Globals.sessionstatus == "connected") // need to be connected to run Force.com queries
            {
                query thisquery = new query();
                thisquery.doquery(); // run a Force.com query
            }


            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        // Load data for the ViewModel Items
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
                if (Globals.firstheadername!="") // we've got a real header name from a Force.com query
                { 
                this.FirstHeader.Header = Globals.firstheadername; // set the header name of the retrieved object
                }
            }
        }

        private void ForceComPivot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            this.ForceComPivot.Title = headtitle + " :: " + Globals.sessionstatus; // current status is set by Force.com auth process

            if (Globals.sessionstatus == "connected" && StoredResults.Contacts == null) // let's try a Force.com query if they're connected & don't yet have stored results
            {
                query thisquery = new query();
                thisquery.doquery(); // run a Force.com query
            }
            
                App.ViewModel.LoadData();
                if (Globals.firstheadername != "") // we've got a real header name
                {
                    this.FirstHeader.Header = Globals.firstheadername; // set the header name of the retrieved Force.com object
                }
            
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}